package com.hit.aircraftwar.setter;

import com.hit.aircraftwar.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class ParameterSet {

    //用户信息
    private String name;
    private int point;
    private final String url = "http://426k04070x.qicp.vip/user/";
    public boolean isOnline = false;
    public static int opponentScore = 0;

    //声音
    private boolean sound;
    private boolean bgm;

    //英雄机种类
    private int heroType;
    //boss属性
    private int bossHp;
    private int bossPower;
    private int bossSpeedX;
    private int baseBossHp;
    private int baseBossPower;
    //精英属性
    private int eliteHp;
    private int elitePower;
    private int eliteSpeedX;
    private int baseEliteHp;
    private int baseElitePower;
    //一般敌机属性
    private int mobHp;
    private int baseMobHp;
    private int mobSpeedY;
    private int maxEnemy;
    //时间周期和分数周期
    private int eliteInterval;
    private int bossScoreInterval;
    private int cycleDuration;
    private int newCycleDuration;
    //难度
    private int difficulty;
    //敌机存在标记
    private boolean hasBoss;
    private boolean hasElite;
    //道具生效标记
    public boolean trackBulletActive;
    public boolean hpDouble;
    public boolean fireDouble;
    public boolean pointDouble;
    //最大速度倍率
    private final double maxSpeedIndex = 3.0;
    //补给生成概率范围
    private double fireSupplyRange;
    private double bombSupplyRange;
    private double hpSupplyRange;
    //背景图片
    private int backGround;
    public static final ParameterSet INSTANCE = new ParameterSet();

    private ParameterSet() {
        trackBulletActive = hpDouble = fireDouble = pointDouble = false;
        difficulty = 0;
        sound = true;
        bgm = true;
        heroType = 1;
        setDifficulty(0);
    }
    public static ParameterSet getInstance() {
        return INSTANCE;
    }

    public void resetMall() {
        trackBulletActive = hpDouble = fireDouble = pointDouble = false;
    }
    public void setDifficulty(int difficulty) {
        this.difficulty = difficulty;
        if(difficulty == 1) {
            bossHp = baseBossHp = 800;
            bossPower = baseBossPower = 15;
            eliteHp = baseEliteHp = 200;
            elitePower = baseElitePower = 10;
            maxEnemy = 5;
            eliteInterval = 10000;
            bossScoreInterval = 720;
            cycleDuration = newCycleDuration = 600;
            baseMobHp = mobHp = 50;
            mobSpeedY = 25;
            hasBoss = false;
            hasElite = false;
            fireSupplyRange = 0.80;
            bombSupplyRange = 0.50;
            hpSupplyRange = 0.20;
            eliteSpeedX = 20;
            backGround = R.drawable.bg4;
        } else if(difficulty == 2) {
            bossHp = baseBossHp = 1000;
            bossPower = baseBossPower = 20;
            eliteHp = baseEliteHp = 300;
            elitePower = baseElitePower = 15;
            maxEnemy = 6;
            eliteInterval = 10000;
            bossScoreInterval = 300;
            cycleDuration = newCycleDuration = 600;
            baseMobHp = mobHp = 60;
            mobSpeedY = 25;
            hasBoss = false;
            hasElite = false;
            fireSupplyRange = 0.75;
            bombSupplyRange = 0.50;
            hpSupplyRange = 0.25;
            eliteSpeedX = 20;
            backGround = R.drawable.bg5;
        } else {
            bossHp = baseBossHp = 500;
            bossPower = baseBossPower = 10;
            eliteHp = baseEliteHp = 180;
            elitePower = baseElitePower = 10;
            maxEnemy = 4;
            eliteInterval = 4800;
            bossScoreInterval = 32767;
            cycleDuration = newCycleDuration = 600;
            baseMobHp = mobHp = 30;
            mobSpeedY = 25;
            hasBoss = false;
            hasElite = false;
            fireSupplyRange = 0.67;
            bombSupplyRange = 0.33;
            hpSupplyRange = 0.00;
            eliteSpeedX = 20;
            backGround = R.drawable.bg;
        }
    }

    public double updateEnemyAttributes(int time) {
        double index = ((double)time / 10e4 + 1);
        bossHp = (int)(index * baseBossHp);
        bossPower = (int)(index * baseBossPower);
        elitePower = (int)(index * baseElitePower);
        eliteHp = (int)(index * baseEliteHp);
        mobHp = (int)(index * baseMobHp);
        return index;
    }

    public int updateCycleDuration(int time) {
        double index = ((double)time / 10e4 + 1);
        if(index > maxSpeedIndex) {
            index = maxSpeedIndex;
        } else if(index <= 0) {
            index = 1.0;
        }
        newCycleDuration = (int)(cycleDuration / index);
        return newCycleDuration;
    }
    public void updateSpeed(int time) {
        double index = ((double)time / 10e4 + 1);
        if(index > maxSpeedIndex) {
            index = maxSpeedIndex;
        } else if(index <= 0) {
            index = 1.0;
        }
        int baseBossSpeedX = 8;
        bossSpeedX = (int)(index * baseBossSpeedX);
        int baseEliteSpeedX = 8;
        eliteSpeedX = (int)(index * baseEliteSpeedX);
        int baseMobSpeedY = 10;
        mobSpeedY = (int)(index * baseMobSpeedY);
    }

    public int getBossScoreInterval() {
        return bossScoreInterval;
    }

    public boolean getHasBoss() {
        return hasBoss;
    }

    public boolean noElite() {
        return !hasElite;
    }

    public int getEliteInterval() {
        return eliteInterval;
    }

    public int getMaxEnemy() {
        return maxEnemy;
    }

    public int getBossSpeedX() {
        return bossSpeedX;
    }

    public int getEliteSpeedX() {
        return eliteSpeedX;
    }

    public int getMobSpeedY() {
        return mobSpeedY;
    }

    public int getNewCycleDuration() {
        return newCycleDuration;
    }

    public int getBossHp() {
        return bossHp;
    }

    public int getBossPower() {
        return bossPower;
    }

    public int getEliteHp() {
        return eliteHp;
    }

    public int getElitePower() {
        return elitePower;
    }

    public int getMobHp() {
        return mobHp;
    }

    public int getDifficulty() {
        return difficulty;
    }

    public void setSound(boolean sound) {
        this.sound = sound;
    }

    public void setBgm(boolean bgm) {
        this.bgm = bgm;
    }

    public boolean getSound() {
        return sound;
    }

    public boolean getBgm() {
        return bgm;
    }
    public void setHasBoss(boolean hasBoss) {
        this.hasBoss = hasBoss;
    }

    public void setHasElite(boolean hasElite) {
        this.hasElite = hasElite;
    }

    public double getFireSupplyRange() {
        return fireSupplyRange;
    }

    public double getBombSupplyRange() {
        return bombSupplyRange;
    }

    public double getHpSupplyRange() {
        return hpSupplyRange;
    }

    public int getBackGround() {
        return backGround;
    }

    public void setTrackBulletActive(boolean trackBulletActive) {
        this.trackBulletActive = trackBulletActive;
    }
    public boolean isTrackBulletActive() {
        return trackBulletActive;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    public void setPoint() {
        OkHttpUtils
                .post()
                .url(url+"getPoint")
                .addParams("id", name)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        point = Integer.parseInt(response);
                    }
                });
    }


    public int getPoint() {
        return point;
    }

    public void updatePoint(int point){
        OkHttpUtils
                .post()
                .url(url+"updatePoint")
                .addParams("id", name)
                .addParams("point", Integer.toString(point))
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {

                    }
                });
    }

    public void setHeroType(int heroType) {
        if(heroType == 1) {
            this.heroType = 1;
        } else {
            this.heroType = 2;
        }
    }

    public int getHeroType() {
        return heroType;
    }

    public String getUrl() {
        return url;
    }

}
